<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad,getEditor, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,errorModal,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				codeCache:'bro-jy-tsampleBorrowApply-productInfo-code-cache'
			})
		},
		methods:{
			onSelectProductReturn(val){
				let _that = this
				let isAfter = false
				if(_that.$refs.autoform.formdata.productBorrowDate){
					isAfter = moment(val).isAfter(_that.$refs.autoform.formdata.productBorrowDate)
				}
				if(!isAfter){
					let title1 = _that.$refs.autoform.getEditorTitle("productInfo","productBorrowDate")
					let title2 = _that.$refs.autoform.getEditorTitle("productInfo","productReturnDate")
					errorModal(_that,'提示信息',title1+'需要大于'+title2+',请重新选择',()=>{
						_that.$refs.autoform.clear('productInfo','productReturnDate')
					})
				}
			},
			inputProduct(val){
				let _that = this
				let productNum = Number(val)
				let  productMoneyOne = Number(_that.$refs.autoform.formdata.productMoneyOne) 
				if(productMoneyOne){
					_that.$refs.autoform.formdata.productMoneyAll = productNum * productMoneyOne
				}
			},
			inputProductMoney(val){
				let _that = this
				let productMoney = Number(val)
				let  productNumber = Number(_that.$refs.autoform.formdata.productNumber) 
				if(productNumber){
					_that.$refs.autoform.formdata.productMoneyAll = productNumber * productMoney
				}
			},
			initLoad(){
				let _that = this
				let arr = [
					_that.om.request(
						"tsysdict/getCombobox",
						{
							code:'36235'
						}
					),
					_that.om.request(
						"tsysdict/getListByCode",
						{
							code:'38057'
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor('productInfo','productUnitId','data',configSelectData(res[0],"id",'dicname'))
					_that.$refs.autoform.initSelectIndex("productInfo","productUnitId")
					_that.$refs.autoform.changeEditor('productInfo','productBorrowTypeId','data',configSelectData(res[1],"id",'dicname'))
					_that.$refs.autoform.initSelectIndex("productInfo","productBorrowTypeId")
				})
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.codeCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							if(Object.prototype.toString.call(d) == '[object Object]'){
								formdata.codeId = d.id
								formdata.codeName = d.projectCode
								formdata.productName = d.projectName
								formdata.productModel = d.productModel
								formdata.productUnitId = d.mainUnitId
								formdata.productUnitName = d.mainUnitName
								_that.$refs.autoform.initSelectIndex("productInfo","productUnitId")
								_that.$refs.autoform.changeEditor("productInfo","productName",'type',"labelbox")
								_that.$refs.autoform.changeEditor("productInfo","productModel",'type',"labelbox")
								_that.$refs.autoform.changeEditor("productInfo","productUnitId","type","labelbox")
							}else{
								_that.$refs.autoform.clear("productInfo","codeId")
								_that.$refs.autoform.clear("productInfo","productName")
								_that.$refs.autoform.changeEditor("productInfo","productModel")
								_that.$refs.autoform.changeEditor("productInfo","productUnitId")
								formdata.codeName = d
							}
							
							uni.removeStorage({
								key:_that.codeCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
								changeEditor("productInfo","codeId",{
									"linkParam":{
										other:JSON.stringify({
										}),
										url:'productShow/likeSerchPager',
										initload:true,
										namefield:'projectCode',
										notefield:'projectName',
										cachename:_that.codeCache
									}
								},null,_that.autoformdata)
								changeEditor(_that.autoformdata[0].editors, 'files', 'uploadUrl', 'tsampleBorrowApply/uploadFile');
								changeEditor(_that.autoformdata[0].editors, "files", "loadUrl", "tsampleBorrowApply/getInterimFile");
								var tempFileList = [];
								var fileUrlList = []
								let editor = getEditor('productInfo','files',_that.autoformdata)
								if (_that.formdata.files){
									fileUrlList = _that.formdata.files.map(t => {
										tempFileList.push(t.fileUrl ||t.fileSaveName);
										return t.fileUrl || t.fileSaveName;
									});
									changeEditor(_that.autoformdata[0].editors, 'files', 'fileUrlList', fileUrlList);
									changeEditor(_that.autoformdata[0].editors, 'files', 'tempFileList', tempFileList);
								}
								if(_that.formdata.fileList){
									let list = fileUrlList.concat(_that.formdata.fileList.map(t => {
										tempFileList.push(t.fileSaveName);
										return t.fileSaveName;
									}))
									changeEditor(_that.autoformdata[0].editors, 'files', 'fileUrlList', list);
									changeEditor(_that.autoformdata[0].editors, 'files', 'tempFileList', tempFileList);
								}
								_that.initLoad()
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
